﻿using EWS;
using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Meeting.Entity.dbEntity;
using XF.Meeting.Entity.DTO;

namespace DYUserCenterSync
{
    public static class ExchangeMeet
    {

        public static void get()
        {


            //  ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
            #region 获取会议信息
            //var service = ExChangeHelper.getExChangeHelper();
            //var meetList = new List<Appointment>();
            //var meetss = service.GetRoomLists();
            var roomService = ExChangeHelper.getExChangeHelper("jiao.yang2@3dmedcare.com");
           var meeting = ExChangeHelper.GetMeeting(roomService, new DateTime(2020, 12, 29,0,0,0), new DateTime(2021, 01, 01,23,40,0));
           var ccc = meeting.ToList();
            //var aaaa = ccc.Where(i => i.ICalUid == "040000008200E00074C5B7101A82E00800000000E490B3FBADD2D601000000000000000010000000ECE4F536A794F743932D1FC3D61FB374").First();
            Appointment cccc = Appointment.Bind(roomService,ccc[2].Id);
     //    var ent=   ExChangeHelper.GetEmailEntity(cccc);
       var mast=     Appointment.BindToRecurringMaster(roomService, ccc[2].Id);
               var UserService = ExChangeHelper.getExChangeHelper(cccc.Organizer.Address);
                       var view = new CalendarView(cccc.Start.AddDays(-1), cccc.End.AddDays(180));
                       view.PropertySet = new PropertySet(AppointmentSchema.ICalUid);
                     //var userMeet = UserService.FindAppointments(WellKnownFolderName.Calendar,view);
                      CalendarFolder calendar = CalendarFolder.Bind(UserService, WellKnownFolderName.Calendar, new PropertySet());
            //    // Set the start and end time and number of appointments to retrieve.


            //    // Limit the properties returned to the appointment's subject, start time, and end time.
             FindItemsResults<Appointment> appointments = calendar.FindAppointments(view);
                   var id = appointments.Where(i => i.ICalUid == cccc.ICalUid).FirstOrDefault();
            //// var pro=  roomService.LoadPropertiesForItems(items, new PropertySet(BasePropertySet));


                     Appointment oApt = Appointment.Bind(UserService, id.Id);
                      cccc=oApt;

            //    //if (oApt.AppointmentType==AppointmentType.Occurrence)
            //    //{
            //    //     cccc = Appointment.BindToRecurringMaster(oApt.Service, oApt.Id);
            //    //}
            //    //if (oApt.AppointmentType==AppointmentType.RecurringMaster)
            //    //{
            //    //     cccc = Appointment.Bind(oApt.Service, oApt.Id);
            //    //}
            //    //if (oApt.AppointmentType==AppointmentType.Exception)
            //    //{
            //    //    cccc = Appointment.BindToRecurringMaster(oApt.Service, oApt.Id);
            //    //}

            //var RecurrenceType = cccc.Recurrence;
            //var day = RecurrenceType as Recurrence.WeeklyPattern;
            //List<ZhouqiEntity> list = new List<ZhouqiEntity>();
            //var type = day.DaysOfTheWeek.ToString();
            //int itemsort = 1;
            //if (day.NumberOfOccurrences is int)
            //{

            //    for (int i = 1; i <= day.NumberOfOccurrences; i++)
            //    {
            //        try
            //        {
            //                var meet=   Appointment.BindToOccurrence(cccc.Service, cccc.Id, i);
            //            var ccccc = ExChangeHelper.GetEmailEntity(meet);

            //            list.Add(new ZhouqiEntity() {
            //                IsDelete = false,
            //                EmailEntity = ccccc,
            //            } );
            //            Console.WriteLine($"ID:{ccccc.itemId},ICalUid:{ccccc.ID}名称：{ccccc.Start}第{i}个元素,时间：{ccccc.Start}-{ccccc.End}");

            //        }
            //        catch (Exception e)
            //        {

            //            var c = e.Message;
            //            list.Add(new ZhouqiEntity()
            //            {
            //                IsDelete = true,
            //                EmailEntity = new EmailEntity()
            //            }); 
            //            Console.WriteLine($"第{i}个元素被删除了");
            //            continue;
            //        }




            //    }
            //}
            //else {
            //var starttime = day.StartDate; var endtime = day.EndDate;
            //    for (int i = 1; starttime <= endtime; i++)
            //    {
            //    try
            //        {
            //            if (type.Contains(starttime.DayOfWeek.ToString()))
            //            {
            //                var meet = Appointment.BindToOccurrence(cccc.Service, cccc.Id, itemsort);
            //                var ccccc = ExChangeHelper.GetEmailEntity(meet);
            //                list.Add(new ZhouqiEntity()
            //                {
            //                    IsDelete = false,
            //                    EmailEntity = ccccc,
            //                });
            //                Console.WriteLine($"ID:{ccccc.itemId},ICalUid:{ccccc.ID}名称：{ccccc.Subject}第{itemsort}个元素,时间：{ccccc.Start}-{ccccc.End}");
            //                itemsort++;
            //            }

            //        starttime=starttime.AddDays(1);

            //    }
            //    catch (Exception e)
            //    {

            //        var c = e.Message;
            //            list.Add(new ZhouqiEntity()
            //            {
            //                IsDelete = true,
            //                EmailEntity = new EmailEntity()
            //            });

            //            starttime = starttime.AddDays(1);
            //            Console.WriteLine($"第{itemsort}个元素,被删除！");
            //            itemsort++;
            //            continue;
            //    }




            //    }


            //}

            #endregion
            //var userService = ExChangeHelper.getExChangeHelper("caifu.chen@3dmedcare.com");
            //Appointment appointment = new Appointment(userService);
            //// Set the properties on the appointment object to create the appointment.
            //appointment.Subject = "Tennis lesson";
            //appointment.Body = "Focus on backhand this week.";
            //appointment.Start = DateTime.Now.AddDays(2);
            //appointment.End = appointment.Start.AddHours(1);
            //appointment.Location = "冥王星";
            //appointment.ReminderDueBy = DateTime.Now;
            //// Save the appointment to your calendar.
            //appointment.Save(SendInvitationsMode.SendToNone);
            //// Verify that the appointment was created by using the appointment's item ID.
            //Item item = Item.Bind(new ExchangeService(), appointment.Id, new PropertySet(ItemSchema.Subject));
            //Console.WriteLine("\nAppointment created: " + item.Subject + "\n");
        }
    }
        }
    

